/**
 * @Title: PackageFlowAdapter.java
 * @Description: TODO
 * @author Mengting Lu
 * @date 2015年8月5日 上午11:51:30
 */
package com.shanghaionstar.carmanage.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.widget.TextView;

import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.more.entity.FlowDataComplexType;
import com.shanghaionstar.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: PackageFlowAdapter
 * @Description: TODO
 * @author Mengting Lu
 * @date 2015年8月5日 上午11:51:30
 */
public class PackageFlowAdapter extends CommonAdapter<FlowDataComplexType> {
	private Context mContext;

	private List<FlowDataComplexType> mPackageDateList;
	SimpleDateFormat df;

	/**
	 * @Description: TODO
	 * @author Mengting Lu
	 * @date 2015年8月5日 上午11:51:58
	 */
	public PackageFlowAdapter(List<FlowDataComplexType> PackageDateList, Context Context) {
		super(PackageDateList, Context);
		mContext = Context;
		mPackageDateList = PackageDateList;
		df = new SimpleDateFormat("yyyy-MM-dd");
	}

	@SuppressWarnings("deprecation")
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	@Override
	public void getView(int position, CommonViewHolder viewHolder) {
		ViewHolder holder = (ViewHolder) viewHolder;
		FlowDataComplexType bean = mPackageDateList.get(position);
		holder.bindData(bean);

//		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//			if (position == 0) {
//				if (position == 0 && mPackageDateList.size() == 1) {// 如果数据是第一个并且只有一个就显示一个蓝色的圆点
//					holder.DatenoStarted.setBackground(mContext.getResources().getDrawable(R.drawable.service_yuan));
//				} else {
//					holder.DatenoStarted.setBackground(mContext.getResources().getDrawable(R.drawable.service_nu_line));
//				}
//			} else if (position == mPackageDateList.size() - 1) {
//				holder.DatenoStarted.setBackground(mContext.getResources().getDrawable(R.drawable.service_nyuan));
//			} else {
//				holder.DatenoStarted.setBackground(mContext.getResources().getDrawable(R.drawable.service_nu));
//			}
//		} else {
//			if (position == 0) {
//				if (position == 0 && mPackageDateList.size() == 1) {// 如果数据是第一个并且只有一个就显示一个蓝色的圆点
//					holder.DatenoStarted.setBackgroundDrawable(mContext.getResources().getDrawable(
//							R.drawable.service_yuan));
//				} else {
//					holder.DatenoStarted.setBackgroundDrawable(mContext.getResources().getDrawable(
//							R.drawable.service_nu_line));
//				}
//			} else if (position == mPackageDateList.size() - 1) {
//				holder.DatenoStarted.setBackgroundDrawable(mContext.getResources()
//						.getDrawable(R.drawable.service_nyuan));
//			} else {
//				holder.DatenoStarted.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.service_nu));
//			}
//		}
//		if (null != bean) {
//			holder.packageDateName.setText(TextUtils.isEmpty(bean.getPackageName()) ? mContext.getString(R.string.package_list) : bean.getPackageName());
//			holder.duration.setText(TextUtils.isEmpty(bean.getPackageName()) ? "" : bean.getDuration());
//			// if ("false".equalsIgnoreCase(bean.getIsActive())) {
//			if ("false".equalsIgnoreCase(bean.getActive())) {
//				holder.expiredDay_text.setText(mContext.getString(R.string.package_duration));
//				holder.expiredDay.setText(bean.getDuration() + mContext.getString(R.string.package_onstar_behind));
//				holder.packageDateName.setTextColor(mContext.getResources().getColor(R.color.profile_font_color));
//				holder.expiredDay.setTextColor(mContext.getResources().getColor(R.color.profile_tr_font_color));
//				holder.expiredDay_text.setTextColor(mContext.getResources().getColor(R.color.profile_tr_font_color));
//				holder.listLayout.setFocusable(true);
//			} else {
//				holder.expiredDay_text.setText(mContext.getString(R.string.package_expire_date));
//				holder.expiredDay.setText(FormatPackageDate(bean.getExpiredDateTime()));
//
//				holder.packageDateName.setTextColor(mContext.getResources().getColor(R.color.profile_font_color));
//				holder.expiredDay.setTextColor(mContext.getResources().getColor(R.color.profile_tr_font_color));
//				holder.expiredDay_text.setTextColor(mContext.getResources().getColor(R.color.profile_tr_font_color));
//				holder.listLayout.setEnabled(true);
//			}
//
//		}
	}

	@Override
	public int layoutId() {
		return R.layout.item_flow_4g;
	}

	@Override
	public CommonViewHolder getViewHolder() {
		return new ViewHolder();
	}

	private class ViewHolder extends CommonViewHolder {
		@FindViewById
		TextView tv_startDate,tv_endDate,tv_packageName;
		public void bindData(FlowDataComplexType bean){
//			tv_startDate.setText(String.format(context.getString(R.string.start_time),FormatPackageDate(bean.getStartDateTime())));
//			tv_endDate.setText(String.format(context.getString(R.string.end_time),FormatPackageDate(bean.getExpiredDateTime())));
			tv_packageName.setText(bean.getPackageName());
			if ("true".equalsIgnoreCase(bean.getActive())) {
				tv_startDate.setText(String.format(context.getString(R.string.start_time),FormatPackageDate(bean.getStartDateTime())));
				tv_endDate.setText(String.format(context.getString(R.string.end_time),FormatPackageDate(bean.getExpiredDateTime())));
			}else {
				tv_startDate.setText("");
				tv_endDate.setText("");
			}
		}
	}

	/**
	 * 字符串转化为date
	 *
	 * @param
	 *            （yyyy-MM-dd+08:00）
	 * @return java.util.Date
	 */

	private Date StringToate(String s) {
		Date date = null;
		if (!TextUtils.isEmpty(s)) {
			if (s.contains("+")) {
				s = s.substring(0, s.indexOf("+"));
			}
			try {
				date = df.parse(s);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return date;
	}

	/**
	 * date格式化yyyy/MM/dd 用于显示到textview
	 * @return 格式为yyyy/MM/dd的字符串
	 */
	private String FormatPackageDate(String s) {
		if (!TextUtils.isEmpty(s)) {
			return df.format(StringToate(s));
		} else {
			return "";
		}
	}
}
